package com.mrobuy.jdvc.dao.impl;

import com.mrobuy.jdvc.dao.JDAccountDao;
import com.mrobuy.jdvc.entity.JDAccount;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.List;

@Repository("jdAccountDao")
public class JDAccountDaoImpl implements JDAccountDao {

    private static Logger LOG = LoggerFactory.getLogger(JDAccountDao.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<JDAccount> list() {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT * from JD_ACCOUNT");
        try {
            List<JDAccount> list = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper(JDAccount.class));
            return list;
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return Collections.EMPTY_LIST;
    }

    @Override
    public JDAccount load(String appkey) {
        try {
            JDAccount account = (JDAccount)jdbcTemplate.queryForObject("select * from JD_ACCOUNT where APPKEY = ?", new Object[]{appkey}, new BeanPropertyRowMapper(JDAccount.class));
            return account;
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return null;
    }

    @Override
    public boolean insert(JDAccount account) {

        // INSERT INTO jingsu.jd_account (APPKEY, APPSECRET, VCCODE, NAME, ACCESS_TOKEN, EXPIRES_IN, REFRESH_TOKEN, SCOPE, OPEN_ID, UID, TIME, TOKEN_TYPE, CODE) VALUES ('1', null, null, null, null, null, null, null, null, null, null, null, null);
        int cnt = jdbcTemplate.update("INSERT INTO JD_ACCOUNT (APPKEY, APPSECRET, VCCODE, NAME, ACCESS_TOKEN, EXPIRES_IN, REFRESH_TOKEN, SCOPE, OPEN_ID, UID, TIME, TOKEN_TYPE, CODE) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
                account.getAppkey(), account.getAppsecret(), account.getVccode(), account.getName(), account.getAccess_token(), account.getExpires_in(), account.getRefresh_token(), account.getScope(), account.getOpen_id(), account.getUid(), account.getTime(), account.getToken_type(), account.getCode());

        return (cnt==1);

    }

    @Override
    public boolean updatess(JDAccount account) {

        String sql = "UPDATE JD_ACCOUNT SET APPSECRET = ?, VCCODE = ?, NAME = ?, ACCESS_TOKEN = ?, EXPIRES_IN = ?, REFRESH_TOKEN = ?, SCOPE = ?, OPEN_ID = ?, UID = ?, TIME = ?, TOKEN_TYPE = ?, CODE = ? WHERE APPKEY = ? ";

        int cnt = jdbcTemplate.update(sql,
                account.getAppsecret(), account.getVccode(), account.getName(), account.getAccess_token(), account.getExpires_in(), account.getRefresh_token(), account.getScope(), account.getOpen_id(), account.getUid(), account.getTime(), account.getToken_type(), account.getCode(), account.getAppkey());

        return (cnt==1);


    }

    @Override
    public JDAccount loadByVendor(String vendor) {
        try {
            JDAccount account = (JDAccount)jdbcTemplate.queryForObject("select * from JD_ACCOUNT where VCCODE = ?", new Object[]{vendor}, new BeanPropertyRowMapper(JDAccount.class));
            return account;
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return null;
    }
}
